package com.fitness.fitness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.convertor.CoachConvertor;
import com.fitness.fitness.mapper.CoachMapper;
import com.fitness.fitness.modal.coach.dto.CoachDto;
import com.fitness.fitness.modal.coach.dto.CoachEditDto;
import com.fitness.fitness.modal.coach.entity.Coach;
import com.fitness.fitness.modal.coach.param.CoachPageQueryParams;
import com.fitness.fitness.modal.coach.vo.CoachVO;
import com.fitness.fitness.service.ICoachService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CoachServiceImpl extends ServiceImpl<CoachMapper, Coach> implements ICoachService {

    private final CoachConvertor convertor;

    @Override
    public Page<CoachVO> getCoachPage(CoachPageQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<Coach> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<Coach> coachBoPage = this.baseMapper.getCoachPage(page, queryParams);

        return convertor.entity2Vo(coachBoPage);
    }

    @Override
    public boolean addCoach(CoachDto coachDto) {
        Coach coach = convertor.dto2entity(coachDto);
        return save(coach);
    }

    @Override
    public boolean updateCoach(CoachDto coachDto) {
        Long userId = coachDto.getUserId();
        Coach coach = convertor.dto2entity(coachDto);
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.baseMapper.update(coach, queryWrapper) > 0;
    }

    @Override
    public Coach getCoachByUid(Long uid) {
        QueryWrapper<Coach> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
